package cn.edu.nju.software.android.layoutHelper;

import java.text.SimpleDateFormat;
import java.util.Date;

import me.maxwin.view.IListviewCallback;
import me.maxwin.view.XListView;
import me.maxwin.view.XListView.IXListViewListener;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.view.View;
import cn.edu.nju.software.android.activity.R;
import cn.edu.nju.software.android.adapter.TimelineListViewAdapter;

public class TimelineLayoutHelper extends LayoutHelper implements IXListViewListener,IListviewCallback{
	private TimelineListViewAdapter adapter;
	private XListView listView;
	private Handler mHandler;

	public TimelineLayoutHelper(Context context) {
		super(context);
	}

	@Override
	public void initialize(View view) {
		listView = (XListView)view.findViewById(R.id.timeline_list);
		listView.setXListViewListener(this);
		mHandler = new Handler();
		adapter = new TimelineListViewAdapter(context,this);

		listView.setAdapter(adapter);
		listView.setXListViewListener(this);
		listView.setPullLoadEnable(false);
	}

	public void resetToTopRow(){
		listView.setSelection(0);
	}
	@Override
	public void onRefresh() {
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				// mAdapter.notifyDataSetChanged();
				adapter.getNewestStatuses();
			}
		}, 2000);
	}

	@Override
	public void onLoadMore() {
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				adapter.loadMoreStatuses();
			}
		}, 2000);
	}

	@SuppressLint("SimpleDateFormat")
	@Override
	public void onComplete() {
		listView.stopRefresh();
		listView.stopLoadMore();
		Date timeNow = new Date();
		SimpleDateFormat format = new SimpleDateFormat("HH:mm");
		String time = format.format(timeNow);
		listView.setRefreshTime(time);
	}

	@Override
	public void disableLoadFunction() {
		listView.setPullLoadEnable(false);
	}

	@Override
	public void enableLoadFunction() {
		listView.setPullLoadEnable(true);
	}
}
